home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / ODF Release 3 / ODFDev / ODF / OS / FWGraphx / FWBitmap.cpp < prev    next >
Encoding:
Text File  |  1996-12-16  |  13.3 KB  |  493 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWBitmap.cpp
  4. //    Release Version:    $ ODF 3 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWBITMAP_H
  13. #include "FWBitmap.h"
  14. #endif
  15.  
  16. #ifndef SLBITMAP_H
  17. #include "SLBitmap.h"
  18. #endif
  19.  
  20. #ifndef SLGRDEF_H
  21. #include "SLGRDef.h"
  22. #endif
  23.  
  24. #ifndef SLGRGLOB_H
  25. #include "SLGrGlob.h"
  26. #endif
  27.  
  28. #ifndef FWMEMHLP_H
  29. #include "FWMemHlp.h"
  30. #endif
  31.  
  32. #ifndef FWCOLOR_H
  33. #include "FWColor.h"
  34. #endif
  35.  
  36. #ifndef FWGC_H
  37. #include "FWGC.h"
  38. #endif
  39.  
  40. #ifndef FWACQUIR_H
  41. #include "FWAcquir.h"
  42. #endif
  43.  
  44. #ifndef FWODGEOM_H
  45. #include "FWODGeom.h"
  46. #endif
  47.  
  48. #ifndef FWGRUTIL_H
  49. #include "FWGrUtil.h"
  50. #endif
  51.  
  52. #ifndef FWWINRES_H
  53. #include "FWWinRes.h"
  54. #endif
  55.  
  56. #ifndef FWPALETE_H
  57. #include "FWPalete.h"
  58. #endif
  59.  
  60. #ifndef SLWINDIB_H
  61. #include "FWWinDIB.h"
  62. #endif
  63.  
  64. #ifndef FWSOMENV_H
  65. #include "FWSOMEnv.h"
  66. #endif
  67.  
  68. #if defined(FW_BUILD_WIN) && !defined(FWGRUTIL_H)
  69. #include "FWGrUtil.h"
  70. #endif
  71.  
  72. #if defined(FW_BUILD_MAC) && !defined(FWRESACC_H)
  73. #include "FWResAcc.h"
  74. #endif
  75.  
  76. #if defined(FW_BUILD_MAC) && !defined(FWPICTUR_H)
  77. #include "FWPictur.h"
  78. #endif
  79.  
  80. #if defined(FW_BUILD_MAC) && !defined(FWRESSIN_H)
  81. #include "FWResSin.h"
  82. #endif
  83.  
  84. #if defined(FW_BUILD_MAC) && !defined(__PICTUTILS__)
  85. #include "PictUtils.h"
  86. #endif
  87.  
  88. // ----- Foundation Includes -----
  89.  
  90. #ifndef FWSTREAM_H
  91. #include "FWStream.h"
  92. #endif
  93.  
  94. #ifdef FW_BUILD_MAC
  95. #pragma segment FWGraphics_Bitmap
  96. #endif
  97.  
  98. //========================================================================================
  99. //    Template instantiation
  100. //========================================================================================
  101.  
  102. #include "FWGrRef.tpp"
  103.  
  104. FW_DEFINE_AUTO_TEMPLATE(FW_TGrRefPtr, FW_HBitmap)
  105.  
  106. #ifdef FW_USE_TEMPLATE_PRAGMAS
  107.  
  108. #pragma template_access public
  109. #pragma template FW_TGrRefPtr<FW_HBitmap>
  110.  
  111. #else
  112.  
  113. template class FW_TGrRefPtr<FW_HBitmap>;
  114.  
  115. #endif
  116.  
  117. //========================================================================================
  118. //    class FW_CBitmap
  119. //========================================================================================
  120.  
  121. FW_DEFINE_AUTO(FW_CBitmap)
  122.     
  123. //----------------------------------------------------------------------------------------
  124. //    FW_PrivAcquireGrRep
  125. //----------------------------------------------------------------------------------------
  126.  
  127. void FW_PrivAcquireGrRep(FW_HBitmap rep)
  128. {
  129.     if (rep != 0)
  130.         FW_PrivBitmap_Acquire(rep);
  131. }
  132.  
  133. //----------------------------------------------------------------------------------------
  134. //    FW_PrivReleaseGrRep
  135. //----------------------------------------------------------------------------------------
  136.  
  137. void FW_PrivReleaseGrRep(FW_HBitmap rep)
  138. {
  139.     if (rep != 0)
  140.         FW_PrivBitmap_Release(rep);
  141. }
  142.  
  143. //----------------------------------------------------------------------------------------
  144. //    FW_CBitmap::FW_CBitmap
  145. //----------------------------------------------------------------------------------------
  146.  
  147. FW_CBitmap::FW_CBitmap()
  148. {
  149.     FW_END_CONSTRUCTOR
  150. }
  151.  
  152. //----------------------------------------------------------------------------------------
  153. //    FW_ValidatePixelSize
  154. //----------------------------------------------------------------------------------------
  155.  
  156. static short FW_ValidatePixelSize(short pixelSize)
  157. {
  158. #ifdef FW_BUILD_MAC
  159.     FW_ASSERT(pixelSize == 0 || pixelSize == 1 || pixelSize == 2 || pixelSize == 4 ||
  160.               pixelSize == 8 || pixelSize == 16 || pixelSize == 24 || pixelSize == 32);
  161.     if (pixelSize == 24)
  162.         pixelSize = 32;
  163. #endif
  164. #ifdef FW_BUILD_WIN
  165.     FW_ASSERT(pixelSize == 0 || pixelSize == 1 || pixelSize == 2 || pixelSize == 4 ||
  166.               pixelSize == 8 || pixelSize == 16 || pixelSize == 24 || pixelSize == 32);
  167.     if (pixelSize == 32)
  168.         pixelSize = 24;
  169. #endif
  170.  
  171.     return pixelSize;
  172. }
  173.  
  174. //----------------------------------------------------------------------------------------
  175. //    FW_CBitmap::FW_CBitmap
  176. //----------------------------------------------------------------------------------------
  177.  
  178. FW_CBitmap::FW_CBitmap(short width, short height, short pixelSize, FW_Palette palette /* = NULL */)
  179. {
  180.     pixelSize = FW_ValidatePixelSize(pixelSize);
  181.  
  182.     FW_PlatformError error;
  183.     FW_HBitmap rep = FW_PrivBitmap_CreateFromBits(width, height, pixelSize, palette,
  184.                         NULL, 0, 0, &error);
  185.     FW_FailOnError(error);
  186.     SetRep(rep);
  187.     FW_END_CONSTRUCTOR
  188. }
  189.  
  190. //----------------------------------------------------------------------------------------
  191. //    FW_CBitmap::FW_CBitmap
  192. //----------------------------------------------------------------------------------------
  193.  
  194. FW_CBitmap::FW_CBitmap(void* image, long imageSize, short rowBytes,
  195.                        short width, short height, short pixelSize, FW_Palette palette /* = NULL */)
  196. {
  197.     FW_ASSERT(image != 0);
  198.  
  199.     pixelSize = FW_ValidatePixelSize(pixelSize);
  200.  
  201.     FW_PlatformError error;
  202.     FW_HBitmap rep = FW_PrivBitmap_CreateFromBits(width, height, pixelSize, palette,
  203.                         image, imageSize, rowBytes, &error);
  204.     FW_FailOnError(error);
  205.     SetRep(rep);
  206.     FW_END_CONSTRUCTOR
  207. }
  208.  
  209. #ifdef FW_BUILD_MAC
  210.  
  211. const FW_SRect FW_CBitmap::gWholePicture    =    {    0, 0, 0, 0    };
  212.  
  213. //----------------------------------------------------------------------------------------
  214. //    FW_CBitmap::FW_CBitmap
  215. //----------------------------------------------------------------------------------------
  216.  
  217. FW_CBitmap::FW_CBitmap(const FW_CPicture& picture, const FW_CColor& fillColor, const FW_CRect& pictPart)
  218. {
  219.     FW_CRect bounds = pictPart;
  220.  
  221. #if 0
  222.     if(&pictPart == &gWholePicture)
  223.         picture.GetPictBounds(bounds);
  224. #else
  225.     if(pictPart == gWholePicture)
  226.         picture.GetPictBounds(bounds);
  227. #endif
  228.     
  229.     FW_PlatformError error;
  230.     FW_HBitmap rep = FW_PrivBitmap_MacCreateFromPicture(picture, fillColor, bounds, &error);
  231.     FW_FailOnError(error);
  232.     SetRep(rep);
  233.     FW_END_CONSTRUCTOR
  234. }
  235.  
  236. #endif
  237.  
  238. #ifdef FW_BUILD_MAC
  239.  
  240. //----------------------------------------------------------------------------------------
  241. //    FW_CBitmap::MacGetAsPicture
  242. //----------------------------------------------------------------------------------------
  243.  
  244. FW_CPicture FW_CBitmap::MacGetAsPicture(const FW_SRect& pictPart) const
  245. {
  246.     FW_CRect bounds = pictPart;
  247. #if 0
  248.     if(&pictPart == &gWholePicture)
  249.         GetBitmapBounds(bounds);
  250. #else
  251.     if (FW_CRect(gWholePicture)==pictPart)
  252.         GetBitmapBounds(bounds);
  253. #endif
  254.  
  255.     FW_PlatformError error;
  256.     FW_HPicture hPict = FW_PrivBitmap_MacGetAsPicture(fRep, bounds, &error);
  257.     FW_FailOnError(error);
  258.  
  259.     FW_CPicture pict;
  260.     pict.SetRep(hPict);
  261.     return pict;
  262. }
  263.  
  264. #endif
  265.  
  266. //----------------------------------------------------------------------------------------
  267. //    FW_CBitmap::FW_CBitmap
  268. //----------------------------------------------------------------------------------------
  269.  
  270. FW_CBitmap::FW_CBitmap(FW_PlatformBitmap platformBitmap)
  271. {
  272.     FW_PlatformError error;
  273.     FW_HBitmap rep = FW_PrivBitmap_CreateFromPlatformBitmap(platformBitmap, &error);
  274.     FW_FailOnError(error);
  275.     SetRep(rep);
  276.     FW_END_CONSTRUCTOR
  277. }
  278.  
  279. //----------------------------------------------------------------------------------------
  280. //    FW_CBitmap::FW_CBitmap
  281. //----------------------------------------------------------------------------------------
  282.  
  283. FW_CBitmap::FW_CBitmap(FW_PResourceFile& resourceFile, FW_ResourceID resID)
  284. {
  285.     FW_PlatformError error;
  286.     FW_HBitmap rep = FW_PrivBitmap_CreateFromResource(resourceFile, resID, &error);
  287.     FW_FailOnError(error);
  288.     SetRep(rep);
  289.     FW_END_CONSTRUCTOR
  290. }
  291.  
  292. //----------------------------------------------------------------------------------------
  293. //    FW_CBitmap::FW_CBitmap
  294. //----------------------------------------------------------------------------------------
  295.  
  296. FW_CBitmap::FW_CBitmap(FW_CReadableStream& stream, FW_Boolean bDIBHeader)
  297. {
  298.     FW_PlatformError error;
  299.     FW_HBitmap rep = FW_PrivBitmap_Read(stream, bDIBHeader, &error);
  300.     FW_FailOnError(error);
  301.     SetRep(rep);
  302.     FW_END_CONSTRUCTOR
  303. }
  304.  
  305. //----------------------------------------------------------------------------------------
  306. //    FW_CBitmap::FW_CBitmap
  307. //----------------------------------------------------------------------------------------
  308.  
  309. FW_CBitmap::FW_CBitmap(const FW_CBitmap& other) :
  310.     FW_TGrRefPtr<FW_HBitmap>(other)
  311. {
  312.     FW_END_CONSTRUCTOR
  313. }
  314.  
  315. //----------------------------------------------------------------------------------------
  316. //    FW_CBitmap::FW_CBitmap
  317. //----------------------------------------------------------------------------------------
  318.  
  319. FW_CBitmap::~FW_CBitmap()
  320. {
  321.     FW_START_DESTRUCTOR
  322. }
  323.  
  324. //----------------------------------------------------------------------------------------
  325. //    FW_CBitmap::operator=
  326. //----------------------------------------------------------------------------------------
  327.  
  328. FW_CBitmap& FW_CBitmap::operator=(const FW_CBitmap& other)
  329. {
  330.     FW_TGrRefPtr<FW_HBitmap>::operator=(other);
  331.     return *this;
  332. }
  333.  
  334. //----------------------------------------------------------------------------------------
  335. //    FW_CBitmap::Copy
  336. //----------------------------------------------------------------------------------------
  337.  
  338. FW_CBitmap FW_CBitmap::Copy(const FW_CRect& srcRect) const
  339. {
  340.     FW_PlatformError error;
  341.     FW_HBitmap rep = FW_PrivBitmap_Copy(fRep, srcRect, &error);
  342.     FW_FailOnError(error);
  343.     
  344.     FW_CBitmap bitmap;
  345.     bitmap.SetRep(rep);
  346.  
  347.     return bitmap;
  348. }
  349.  
  350. //----------------------------------------------------------------------------------------
  351. //    FW_CBitmap::GetPalette
  352. //----------------------------------------------------------------------------------------
  353.  
  354. FW_Palette FW_CBitmap::GetPalette() const
  355.     FW_PlatformError error;
  356.     FW_Palette palette = FW_PrivBitmap_GetPalette(fRep, &error);
  357.     FW_FailOnError(error);
  358.     return palette;
  359. }
  360.  
  361. //----------------------------------------------------------------------------------------
  362. //    FW_CBitmap::GetBitmapBounds
  363. //----------------------------------------------------------------------------------------
  364.  
  365. void FW_CBitmap::GetBitmapBounds(FW_SGraphicContext& gc, FW_SRect& bounds) const
  366.     FW_PrivBitmap_GetBitmapBoundsGC(gc.fEnvironment, fRep, gc, bounds); 
  367.     FW_FailOnEvError(gc.fEnvironment);
  368. }
  369.         
  370. //========================================================================================
  371. //    Stream I/O
  372. //========================================================================================
  373.  
  374. //----------------------------------------------------------------------------------------
  375. //    operator>>
  376. //----------------------------------------------------------------------------------------
  377.  
  378. FW_CReadableStream& operator>>(FW_CReadableStream& stream, FW_CBitmap& bitmap)
  379. {
  380.     FW_PlatformError error;
  381.     FW_HBitmap rep = FW_PrivBitmap_Read(stream, TRUE, &error);
  382.     FW_FailOnError(error);
  383.     bitmap.SetRep(rep);
  384.     return stream;
  385. }
  386.  
  387. //----------------------------------------------------------------------------------------
  388. //    operator<<
  389. //----------------------------------------------------------------------------------------
  390.  
  391. FW_CWritableStream& operator<<(FW_CWritableStream& stream, const FW_CBitmap& bitmap)
  392. {
  393.     FW_PlatformError error;
  394.     FW_PrivBitmap_Write(bitmap.fRep, stream, TRUE, &error);
  395.     FW_FailOnError(error);
  396.     return stream;
  397. }
  398.  
  399. //========================================================================================
  400. //    class FW_CBitmapContext
  401. //========================================================================================
  402.  
  403. FW_DEFINE_AUTO(FW_CBitmapContext)
  404.  
  405. //----------------------------------------------------------------------------------------
  406. //    FW_CBitmapContext::FW_CBitmapContext
  407. //----------------------------------------------------------------------------------------
  408.  
  409. FW_CBitmapContext::FW_CBitmapContext(Environment* ev, FW_CBitmap& bitmap) :
  410.     FW_CGraphicContext(ev),
  411.     fBitmap(bitmap),
  412.     fGraphicDevice(NULL)
  413. {
  414.     FW_ASSERT(bitmap != NULL);
  415.     FW_PlatformBitmap    platformBitmap = bitmap.GetPlatformBitmap();
  416.     FW_PlatformCanvas    platformCanvas;
  417.  
  418. #ifdef FW_BUILD_WIN
  419.     // [jkp]    Create a platform canvas from an HDC
  420.     HDC anHDC = ::CreateCompatibleDC(NULL);
  421.     platformCanvas = FW_PrivGDev_WinCreateFromHDC(ev, anHDC);
  422.  
  423.     fOldBitmap = (HBITMAP) ::SelectObject(anHDC, platformBitmap);
  424.     if (fOldBitmap == NULL)
  425.         FW_Failure(FW_xGraphicException);
  426. #endif
  427. #ifdef FW_BUILD_MAC
  428.     platformCanvas = (FW_PlatformCanvas) platformBitmap;
  429.     bitmap.MacLockPixels();
  430. #endif
  431.     fGraphicDevice = FW_PrivGDev_CreateFromPlatformCanvas(ev, platformCanvas);
  432.     FW_FailOnEvError(ev);
  433.     FW_PrivGDev_SetResolution(fGraphicDevice, FW_kFixed72, FW_kFixed72);
  434.  
  435.     FW_CRect bounds;
  436.     bitmap.GetBitmapBounds(bounds);
  437.     
  438.     FW_CAcquiredODShape aqClipShape = ::FW_NewODShape(ev, bounds);
  439.  
  440.     InitGraphicContext(fGraphicDevice,
  441.                        NULL,
  442.                        aqClipShape);
  443.  
  444.     FW_END_CONSTRUCTOR
  445. }                      
  446.  
  447. //----------------------------------------------------------------------------------------
  448. //    FW_CBitmapContext::~FW_CBitmapContext
  449. //----------------------------------------------------------------------------------------
  450.  
  451. FW_CBitmapContext::~FW_CBitmapContext()
  452. {
  453.     FW_START_DESTRUCTOR
  454. #ifdef FW_BUILD_MAC
  455.     fBitmap.MacUnlockPixels();
  456. #endif
  457.  
  458. #ifdef FW_BUILD_WIN
  459.     // Save the DC for later use
  460.     FW_SOMEnvironment ev;
  461.     FW_PlatformCanvas aPlatformCanvas = FW_PrivGDev_GetPlatformCanvas(fGraphicDevice);
  462.     HDC anHDC = aPlatformCanvas->GetDC(ev);
  463.  
  464.     aPlatformCanvas->ReleaseDC(ev);
  465. #endif
  466.  
  467.     // ----- Terminate the superclass
  468.     TerminateGraphicContext();
  469.  
  470. #ifdef FW_BUILD_WIN
  471.     ::SelectObject(anHDC, fOldBitmap);
  472.     ::DeleteDC(anHDC);
  473. #endif
  474.  
  475.     // ----- Delete the device
  476.     FW_PrivGDev_Release(fGraphicDevice);
  477.  
  478. #ifdef FW_BUILD_WIN
  479.     delete aPlatformCanvas;
  480. #endif
  481. }
  482.  
  483. //----------------------------------------------------------------------------------------
  484. //    FW_CBitmapContext::Reset
  485. //----------------------------------------------------------------------------------------
  486.  
  487. void FW_CBitmapContext::Reset()
  488. {
  489.     // Nothing to do [HLX] Am i sure ?
  490. }
  491.